package cn.mux.jdk8.thread;

import java.util.concurrent.locks.ReentrantLock;

public class Account {
	private final ReentrantLock lock = new ReentrantLock();
	private final ReentrantLock lock2 = new ReentrantLock();
	private String  accountNo;
	private double balance;
	
	public Account(String accountNo,double balance){
		this.accountNo  = accountNo;
		this.balance = balance;
	}

	String getAccountNo() {
		return accountNo;
	}
	

	double getBalance() {
		return balance;
	}
	
	public void draw(double drawAmout){
		lock2.lock();
		try {
			if (balance >= drawAmout) {
				System.out.println(Thread.currentThread().getName() + "取钱成功！吐出钞票：" + drawAmout);
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				balance -= drawAmout;
				System.out.println("\t 余额为:" + balance);
			} else {
				System.out.println(Thread.currentThread().getName() + "取钱失败");
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			lock2.unlock();
		}
		
	}
	
	
	public void randomNum(){
		lock.lock();
		try {
			for (int i = 0; i < 200; i++) {
				System.out.println(Thread.currentThread().getName() + " " + i);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			lock.unlock();
		}
	}
	
	
	
	
	
	
}
